<?php
declare (strict_types=1);

namespace app\sysuser\controller\system;

use app\common\controller\SysBackend;
use app\common\model\banxi\sys\SysConfig as SysConfigModel;
use app\common\service\ConfigService;
use think\db\exception\DbException;
use think\facade\Cache;

class SysConfig extends SysBackend
{
    protected array $noNeedLogin = [];
    protected array $noNeedPermission = [];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new SysConfigModel();
    }


    /**
     * 修改
     * @return void
     * @throws DbException
     */
    public function batch(): void
    {
        $param = $this->request->filter('trim')->param();
        $configList = $param['configList'];
        foreach ($configList as $item) {
            $configKey = $item['configKey'];
            if ($this->model->where('configKey', $configKey)->count() > 0) {
                $this->model->update($item, ['configKey' => $configKey]);
            } else {
                $this->model->save($item);
            }
        }
        Cache::tag('sys_config')->clear();
        $this->ok('操作成功', [], self::NOTIFICATION);
    }

    /**
     * 查询
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function listJson()
    {
        $keys = $this->request->param('keys');
        $ConfigService = new ConfigService();
        $data = $ConfigService->getSysConfigValueList($keys);
        $this->ok(__('Operation completed'), $data);
    }
}
